package com.ktjy.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class TransactionAspect {
    private static final Logger log = LoggerFactory.getLogger(TransactionAspect.class);


    @Before(value = "execution(* com.ktjy.service..*.*(..))")
    public void before(JoinPoint jp) {
        log.info("模拟打开数据库");
        log.info("SqlSession sqlSession = MyBatisUtils.createSqlSession(false);");
    }

    @AfterReturning(value = "execution(* com.ktjy.service..*.*(..))",returning = "result")
    private void afterReturning(JoinPoint jp,Object result) {
        log.info("模拟正常完成业务提交事务");
        log.info("session.commit();");
    }

    @AfterThrowing(value = "execution(* com.ktjy.service..*.*(..))",throwing = "e")
    public void afterThrowing(JoinPoint jp,Throwable e) {
        log.info("模拟业务回滚");
        log.info("session.rollback();");
    }

    public void after(JoinPoint jp) {
        log.info("模拟业务完成释放资源...");
        log.info("MyBatisUtil.closeSqlSession(sqlSession);");
    }
}
